<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'addmenusitem' })"
          >添加菜单</el-button
        >
      </div>
      <el-table :data="menuData" style="width: 100%">
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column prop="name" label="菜单名称"> </el-table-column>
        <el-table-column prop="level" label="菜单级数"></el-table-column>
        <el-table-column prop="icon" label="菜单图标"> </el-table-column>
        <el-table-column prop="orderNum" label="排列"> </el-table-column>
        <el-table-column label="操作">
          <!-- 这里拿到id：记住（是死的）就这么写 -->
          <template v-slot="scoped">
            <el-button
              @click="
                $router.push({
                  name: 'menus_edit',
                  params: { id: scoped.row.id },
                })
              "
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteMenuItem(scoped.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllMenus, deleteMenu } from "@/api/menu";
export default {
  name: "Menu",
  data() {
    return {
      // 菜单数据
      menuData: null,
    };
  },
  created() {
    this.loadMenus();
  },
  methods: {
    loadMenus() {
      getAllMenus().then((res) => {
        console.log(res);
        // 获取数据成功
        if (res.data.code === "000000") {
          this.menuData = res.data.data;
        } else {
          this.$message.error("获取菜单列表失败！");
        }
      });
    },
    deleteMenuItem(id) {
      this.$confirm("确定删除此项吗？", "删除提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
      })
        .then(() => {
          console.log(id);
          deleteMenu(id).then((res) => {
            console.log(res);
            if (res.data.data) {
              this.$message.success("删除菜单成功！");
              // 更新数据
              this.loadMenus();
            } else {
              this.$message.error("删除失败，您没有权限！");
            }
          });
        })
        .catch(() => {
          this.$message.error("已取消删除菜单！");
        });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
